package com.zadtek.shopmall.activities;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.goyourfly.multi_picture.ImageLoader;
import com.goyourfly.multi_picture.MultiPictureView;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsActivity;
import com.zadtek.shopmall.bean.OperateResponseBean;
import com.zadtek.shopmall.bean.ProductItem;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.utils.LogUtil;
import com.zadtek.shopmall.utils.ToastUtil;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.nereo.multi_image_selector.MultiImageSelector;

public class ApplyServiceActivity extends AbsActivity implements View.OnClickListener {
    private ImageView ivProductImg;
    private TextView tvProductTitle;
    private TextView tvProductPrice;
    private TextView tvProductNum;
    private TextView tvBtnReturn;
    private TextView tvBtnChange;
    private TextView tvBtnFix;
    private TextView tvReasonTitle;
    private EditText etReasonTitle;
    private TextView tvReasonDesc;
    private EditText etReasonDesc;
    private MultiPictureView multipleImage;
    private ImageView ivBack;
    private TextView title;
    private TextView tvBtnApply;
    private ProductItem productItem;

    private void initView() {
        tvBtnApply = (TextView)findViewById(R.id.tv_btn_apply);
        ivBack = (ImageView)findViewById(R.id.ic_back);
        ivBack.setVisibility(View.VISIBLE);
        title = (TextView) findViewById(R.id.title);
        title.setText("售后申请");
        ivProductImg = (ImageView)findViewById( R.id.iv_product_img );
        tvProductTitle = (TextView)findViewById( R.id.tv_product_title );
        tvProductPrice = (TextView)findViewById( R.id.tv_product_price );
        tvProductNum = (TextView)findViewById( R.id.tv_product_num );
        tvBtnReturn = (TextView)findViewById( R.id.tv_btn_return );
        tvBtnChange = (TextView)findViewById( R.id.tv_btn_change );
        tvBtnFix = (TextView)findViewById( R.id.tv_btn_fix );
        tvReasonTitle = (TextView)findViewById( R.id.tv_reason_title );
        etReasonTitle = (EditText)findViewById( R.id.et_reason_title );
        tvReasonDesc = (TextView)findViewById( R.id.tv_reason_desc );
        etReasonDesc = (EditText)findViewById( R.id.et_reason_desc );
        multipleImage = (MultiPictureView)findViewById( R.id.multiple_image );
    }

    @Override
    public int setView() {
        return R.layout.activity_apply_service;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        productItem = (ProductItem) getIntent().getSerializableExtra("data");
        if(productItem == null){
            ToastUtil.showToast("你没传数据到该页面！");
        }
        initView();
        initLogic();
        updateView();
    }

    private void updateView() {
        Glide.with(this).load(productItem.getProductPic()).placeholder(R.drawable.placeholder).into(ivProductImg);
        tvProductTitle.setText(productItem.getProductTitle());
        tvProductNum.setText("X "+String.valueOf(productItem.getProductNum()));
        tvProductPrice.setText(String.valueOf(productItem.getProductPrice()));
    }

    private void initLogic() {
        MultiPictureView.setImageLoader(new ImageLoader() {
            @Override
            public void loadImage(@NotNull ImageView image, @NotNull Uri uri) {
                Glide.with(ApplyServiceActivity.this)
                        .load(uri)
                        .placeholder(R.drawable.placeholder)
                        .error(R.drawable.placeholder)
                        .into(image);
            }
        });
        tvBtnApply.setOnClickListener(this);
        tvBtnReturn.setOnClickListener(this);
        tvBtnFix.setOnClickListener(this);
        ivBack.setOnClickListener(this);
        tvBtnChange.setOnClickListener(this);
        multipleImage.setAddClickCallback(new MultiPictureView.AddClickCallback() {
            @Override
            public void onAddClick(View view) {
                performCodeWithPermission("需要相机权限", new PermissionCallback() {
                    @Override
                    public void hasPermission() {
                        MultiImageSelector.create().showCamera(true).multi().count(9).start(ApplyServiceActivity.this,200);
                    }

                    @Override
                    public void noPermission() {

                    }
                }, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK && requestCode == 200){
            ArrayList<String> list = data.getStringArrayListExtra(MultiImageSelector.EXTRA_RESULT);
            LogUtil.d(list.get(0));
            ToastUtil.showToast(list.get(0));
            for(String path:list){
                File file = new File(path);
                Uri uri = Uri.fromFile(file);
                multipleImage.addItem(uri);
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ic_back:
                finish();
                break;
            case R.id.tv_btn_apply:
                apply();
                break;
            case R.id.tv_btn_return:
                resetText();
                select(tvBtnReturn,"退货");
                break;
            case R.id.tv_btn_fix:
                resetText();
                select(tvBtnFix,"维修");
                break;
            case R.id.tv_btn_change:
                resetText();
                select(tvBtnChange,"换货");
                break;
        }
    }

    private void apply() {
        Map<String, Object> param = NetTool.newParams();

        NetTool.getApi().applyAftermarketService(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<OperateResponseBean>>>() {
                    @Override
                    public void onData(BaseResponse<List<OperateResponseBean>> value) {

                    }
                });
    }

    private void select(TextView current,String text) {
        current.setTextColor(getResources().getColor(R.color.color_red));
        current.setBackground(getResources().getDrawable(R.drawable.bg_rad_red));

        tvReasonTitle.setText(text+"原因");
        tvReasonDesc.setText(text+"说明");

    }

    private void resetText() {
        tvBtnChange.setTextColor(getResources().getColor(R.color.color_text_grey2nd));
        tvBtnChange.setBackground(getResources().getDrawable(R.drawable.bg_rad));

        tvBtnFix.setTextColor(getResources().getColor(R.color.color_text_grey2nd));
        tvBtnFix.setBackground(getResources().getDrawable(R.drawable.bg_rad));

        tvBtnReturn.setTextColor(getResources().getColor(R.color.color_text_grey2nd));
        tvBtnReturn.setBackground(getResources().getDrawable(R.drawable.bg_rad));


    }
}
